<?php

namespace app\model\user;

use app\base\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 模型-UserProtocolModel
 * Class UserProtocolModel
 * @package app\model
 */
class UserProtocolModel extends BaseModel
{
    use SoftDelete;

    protected $name = 'user_protocol';

    /**
     * 范围查询-keyword
     *
     * @param [type] $query
     * @param [type] $keyword
     * @return void
     */
    public function scopeKeyword($query, $keyword)
    {
        if (!empty($keyword)) {
            $query->where('title', 'like', "%{$keyword}%");
        }
    }

    /**
     * 范围查询-type
     * @param $query
     * @param $type
     */
    public function scopeType($query, $type)
    {
        if (!empty($type)) {
            $query->where('type', $type);
        }
    }

    /**
     * 范围查询-status
     *
     * @param [type] $query
     * @param [type] $status
     * @return void
     */
    public function scopeStatus($query, $status)
    {
        if (!empty($status)) {
            $query->where('status', $status);
        }
    }
    /**
     * 获取协议
     * @param int $type
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getProtocol($type = 1)
    {
        $map = [
            'type' => $type,
            'status' => 1,
        ];
        $protocol = $this->where($map)->order('update_time DESC')->find();
        return $protocol;
    }


    /**
     * 获取分页
     * @param $keyword
     * @param $status
     * @param $type
     * @param $current_page
     * @param $list_rows
     * @param string $field
     * @return array
     */
    public function getPageData($keyword, $status, $type, $current_page, $list_rows, $field = 'id,create_time,update_time,title,type,status,versions')
    {
        $dataList = $this
            ->field($field)
            ->keyword($keyword)
            ->status($status)
            ->type($type)
            ->order('id desc,create_time desc')
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $items = $dataList->items();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $items];
        return $ret;
    }


}